//
//  XQOrderListPageCell.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQOrderListPageCell.h"
#import "XQCustomEngeLabel.h"
#import "XQCustomPriceView.h"
#import "XQOrderListPageLabels.h"

@interface XQOrderListPageCell()
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) XQCustomEngeLabel *agreeLb;
@property (nonatomic,strong) XQCustomPriceView *priceView;
@property (nonatomic,strong) XQOrderListPageLabels *termLb;
@property (nonatomic,strong) XQOrderListPageLabels *rateLb;
@property (nonatomic,strong) XQOrderListPageLabels *dateLb;
@property (nonatomic,strong) UIImageView *logoImageView;
@property (nonatomic,strong) UILabel *logoLb;
@property (nonatomic,strong) UILabel *descLb;
@property (nonatomic,strong) UIButton *loanBtn;
@end

@implementation XQOrderListPageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self.contentView addSubview:self.bgImageView];
    [self.bgImageView addSubview:self.agreeLb];
    [self.bgImageView addSubview:self.priceView];
    [self.bgImageView addSubview:self.termLb];
    [self.bgImageView addSubview:self.rateLb];
    [self.bgImageView addSubview:self.dateLb];
    [self.bgImageView addSubview:self.logoImageView];
    [self.bgImageView addSubview:self.logoLb];
    [self.bgImageView addSubview:self.descLb];
    [self.bgImageView addSubview:self.loanBtn];
}

- (void)makeConstraints {
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.bottom.equalTo(@-15);
    }];
    
    [self.agreeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@14);
        make.width.mas_equalTo(116);
        make.height.mas_equalTo(24);
    }];
    
    [self.priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.equalTo(self.agreeLb.mas_right).offset(25);
        make.right.equalTo(@-125);
    }];
    
    [self.termLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@52);
        make.left.equalTo(@10);
    }];
    
    [self.rateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.termLb.mas_bottom).offset(3);
        make.left.equalTo(@10);
    }];
    
    [self.dateLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.rateLb.mas_bottom).offset(3);
        make.left.equalTo(@10);
    }];
    
    [self.logoLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@13);
        make.right.equalTo(@-11);
        make.height.mas_equalTo(22);
        make.width.mas_equalTo(74);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.logoLb);
        make.right.equalTo(self.logoLb.mas_left).offset(-4);
        make.width.height.equalTo(@22);
    }];
    
    [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoLb.mas_bottom).offset(12);
        make.right.equalTo(@-23.5);
    }];
    
    [self.loanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.descLb);
        make.top.equalTo(self.descLb.mas_bottom).offset(11);
        make.width.equalTo(@90);
        make.height.equalTo(@26);
    }];
}

#pragma mark - setter

- (void)setImageName:(NSString *)imageName {
    self.bgImageView.image = [UIImage imageNamed:imageName];
}

- (void)setLogoImageName:(NSString *)logoImageName {
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:logoImageName]];
}

- (void)setLogoTitle:(NSString *)logoTitle {
    self.logoLb.text = logoTitle;
}

- (void)setTitle:(NSString *)title {
    self.descLb.text = title;
}

- (void)setPrice:(NSString *)price {
    if ([price containsString:@" "]) {
        NSArray *splitArr = [price componentsSeparatedByString:@" "];
        if (splitArr.count == 2) {
            [self.priceView setPrice:[XQUnitChange unitChange:splitArr[1]]];
            [self.priceView setDollor:splitArr[0]];
        }
    }else {
        [self.priceView setPrice:@""];
        [self.priceView setDollor:@""];
    }
}

- (void)setEffects:(NSArray *)effects {
    _effects = effects;

    if (effects.count > 0) {
        for (int i = 0 ; i < effects.count; i++) {
            NSDictionary *dic = effects[i];
            if (i == 0) {
                [self.termLb setLeft:dic[@"won"]];
                [self.termLb setRight:dic[@"cause"]];
            }else if (i == 1) {
                [self.rateLb setLeft:dic[@"won"]];
                [self.rateLb setRight:dic[@"cause"]];
            }else {
                [self.dateLb setLeft:dic[@"won"]];
                [self.dateLb setRight:dic[@"cause"]];
            }
        }
    }
}

- (void)setPrivacy:(NSString *)privacy {
    self.agreeLb.text = privacy;
    
    [self.agreeLb mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(privacy.length == 0 ? 0 : 116);
    }];
    
    [self.priceView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.agreeLb.mas_right).offset(privacy.length == 0 ? 10 : 25);
    }];
}

#pragma mark - private

- (void)agreeClick {
    if (self.agreeHandle) {
        self.agreeHandle();
    }
}

- (void)loadClick {
    if (self.loanHandle) {
        self.loanHandle();
    }
}

#pragma mark - lazy

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] init];
        [_bgImageView setUserInteractionEnabled:YES];
    }
    return _bgImageView;
}

- (XQCustomEngeLabel *)agreeLb {
    if (!_agreeLb) {
        _agreeLb = [[XQCustomEngeLabel alloc] initWithFrame:CGRectMake(0, 14, 116, 24)];
        _agreeLb.textColor = [UIColor whiteColor];
        _agreeLb.backgroundColor = kBlueTextColor;
        _agreeLb.textAlignment = NSTextAlignmentCenter;
        _agreeLb.font = [UIFont boldSystemFontOfSize:12];
        
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_agreeLb.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
        CAShapeLayer *mask = [CAShapeLayer layer];
        mask.path = path.CGPath;
        _agreeLb.layer.mask = mask;
        [_agreeLb setUserInteractionEnabled:YES];
        [_agreeLb addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(agreeClick)]];
    }
    return _agreeLb;
}

- (XQCustomPriceView *)priceView {
    if (!_priceView) {
        _priceView = [[XQCustomPriceView alloc] init];
    }
    return _priceView;
}

- (XQOrderListPageLabels *)termLb {
    if (!_termLb) {
        _termLb = [[XQOrderListPageLabels alloc] init];
    }
    return _termLb;
}

- (XQOrderListPageLabels *)rateLb {
    if (!_rateLb) {
        _rateLb = [[XQOrderListPageLabels alloc] init];
        
    }
    return _rateLb;
}

- (XQOrderListPageLabels *)dateLb {
    if (!_dateLb) {
        _dateLb = [[XQOrderListPageLabels alloc] init];
    }
    return _dateLb;
}

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
        _logoImageView.image = [UIImage imageNamed:@"order_list_logo"];
    }
    return _logoImageView;
}

- (UILabel *)logoLb {
    if (!_logoLb) {
        _logoLb = [[UILabel alloc] init];
        _logoLb.textColor = [UIColor whiteColor];
        _logoLb.font = [UIFont boldSystemFontOfSize:16];
        _logoLb.adjustsFontSizeToFitWidth = YES;
    }
    return _logoLb;
}

- (UILabel *)descLb {
    if (!_descLb) {
        _descLb = [[UILabel alloc] init];
        _descLb.textColor = [UIColor whiteColor];
        _descLb.font = [UIFont systemFontOfSize:12];
    }
    return _descLb;
}

- (UIButton *)loanBtn {
    if (!_loanBtn) {
        _loanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loanBtn setTitle:@"loan" forState:UIControlStateNormal];
        [_loanBtn setTitleColor:kBlackTextColor forState:UIControlStateNormal];
        _loanBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _loanBtn.backgroundColor = [UIColor whiteColor];
        _loanBtn.layer.cornerRadius = 13;
        [_loanBtn addTarget:self action:@selector(loadClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loanBtn;
}

@end
